from rest_framework.decorators import action
from rest_framework.generics import ListAPIView,RetrieveAPIView
from meiduo_admin.my_paginate import MyPageNumberPagination
from meiduo_admin.serializers import orders_serializers
from orders.models import OrderInfo
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework.response import Response

"""
#1,获取订单信息
class OrdersView(ListAPIView):
    pagination_class = MyPageNumberPagination
    serializer_class = orders_serializers.OrdersSerializer
    # queryset = OrderInfo.objects.all()

    def get_queryset(self):
        #1,获取过滤关键字
        keyword = self.request.query_params.get("keyword")

        #2,判断关键字
        if keyword:
            return OrderInfo.objects.filter(order_id__contains=keyword)
        else:
            return OrderInfo.objects.all()

#2,获取单个订单信息
class OrderView(RetrieveAPIView):
    serializer_class = orders_serializers.OrderSerializer
    queryset = OrderInfo.objects.all()
"""

#3,将获取所有,和单个订单合并到一起
class OrdersReadOnlyView(ReadOnlyModelViewSet):
    pagination_class = MyPageNumberPagination
    serializer_class = orders_serializers.OrderSerializer
    def get_queryset(self):
        #1,获取过滤关键字
        keyword = self.request.query_params.get("keyword")

        #2,判断关键字
        if keyword:
            return OrderInfo.objects.filter(order_id__contains=keyword)
        else:
            return OrderInfo.objects.all()

    #1,修改订单状态
    @action(methods=['PUT'],detail=True)
    def status(self,request,pk): #格式: orders/{pk}/status
        #1,获取参数
        status = request.data.get("status")
        order = self.get_object()

        #2,校验参数
        if not status:
            return Response(status=400)

        #3,数据入库
        order.status = status
        order.save()

        #4,返回响应
        return Response(status=200)